/* 
	该文件专门为Count组件生成action对象
*/
import {
  SET_MENU_LIST,
  SET_ROLE_LIST,
  SET_BUTTON_PERMISSION_LIST,
  SET_USER_INFO,
  SET_SHOULD_INIT
} from '../constant'

import $axios from '@/plugins/axios'
import { $basics } from '@/plugins/eng_config'
import { $getToken } from '@/plugins/baseInfo'

//同步action，就是指action的值为Object类型的一般对象
export const setMenuList = data => ({
  type: SET_MENU_LIST,
  data
})

export const setRoleList = data => ({
  type: SET_ROLE_LIST,
  data
})

export const setButtonPermissionList = data => ({
  type: SET_BUTTON_PERMISSION_LIST,
  data
})

export const setUserInfo = data => ({
  type: SET_USER_INFO,
  data
})

export const setShouldInit = data => ({
  type: SET_SHOULD_INIT,
  data
})

//异步action，就是指action的值为函数,异步action中一般都会调用同步action，异步action不是必须要用的。
export const initDataAsync = (data) => { // 初始化数据 的  action
	return (dispatch) => {
    return $axios.post(`${$basics}/basics/account/user`, {
      token: $getToken()
    }).then((res = {}) => {
      const { buttonPermissionList = [], menuList = [], roleList = [] } = res.body || {}
      dispatch(setMenuList(menuList))
      dispatch(setButtonPermissionList(buttonPermissionList))
      dispatch(setRoleList(roleList))
      dispatch(setUserInfo(res.body || {}))
      dispatch(setShouldInit(true))
      // setTimeout(() => {
      //   dispatch(setShouldInit(true))
      // }, 1000)
      return '返回的值。。。'
    })
	}
}